﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System
{
    /// <summary>
    /// 程序是不是大小端运行
    /// </summary>
    public static class EndianExtensions
    {
      /// <summary>
      /// 当前是小端
      /// </summary>
        private static bool _ISLittleEndian = true;
      /// <summary>
      /// 当前是不是小端
      /// </summary>
        public static bool ISLittleEndian
        {
            get { return _ISLittleEndian; }
        }


        /// <summary>
        /// 如果当前端不是小端则进行转换
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static short CheckEndianTransform(this short n)
        {
            if (ISLittleEndian)
            {
                return n;
            }
            return n.SwapInt16();
        }
        /// <summary>
        ///  如果当前端不是小端则进行转换
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static ushort CheckEndianTransform(this ushort n)
        {
            if (ISLittleEndian)
            {
                return n;
            }
            return n.SwapUInt16();
        }
        /// <summary>
        ///  如果当前端不是小端则进行转换
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static int CheckEndianTransform(this int n)
        {
            if (ISLittleEndian)
            {
                return n;
            }
            return n.SwapInt32();
        }
        /// <summary>
        ///  如果当前端不是小端则进行转换
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static uint CheckEndianTransform(this uint n)
        {
            if (ISLittleEndian)
            {
                return n;
            }
            return n.SwapUInt32();
        }
        /// <summary>
        ///  如果当前端不是小端则进行转换
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static long CheckEndianTransform(this long n)
        {
            if (ISLittleEndian)
            {
                return n;
            }
            return n.SwapInt64();
        }
        /// <summary>
        ///  如果当前端不是小端则进行转换
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static ulong CheckEndianTransform(this ulong n)
        {
            if (ISLittleEndian)
            {
                return n;
            }
            return n.SwapUInt64();
        }

    }
}
